package com.ruoyi.cms.controller;

import com.ruoyi.cms.domain.CmsContent;
import com.ruoyi.cms.service.ICmsContentService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 文章Controller
 *
 * @author houyong
 * @date 2025-11-08
 */
@Tag(name = "CmsContentController", description = "文章管理 for管理后台")
@RestController
@RequestMapping("cms/manager/content")
public class CmsContentController extends BaseController {
    @Autowired
    private ICmsContentService cmsContentService;

    /**
     * 查询文章列表
     */
//    @RequiresPermissions("cms:content:list")
    @GetMapping("/list")
    public TableDataInfo list(CmsContent cmsContent) {
        startPage();
        List<CmsContent> list = cmsContentService.selectCmsContentList(cmsContent);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
//    @RequiresPermissions("cms:content:export")
    @Log(title = "导出文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsContent cmsContent) {
        List<CmsContent> list = cmsContentService.selectCmsContentList(cmsContent);
        ExcelUtil<CmsContent> util = new ExcelUtil<CmsContent>(CmsContent.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
//    @RequiresPermissions("cms:content:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        CmsContent cmsContent = cmsContentService.selectCmsContentById(id);
        if (cmsContent != null) {
            cmsContentService.updateReadCountById(id);
        }
        return success(cmsContent);
    }

    /**
     * 新增文章
     */
//    @RequiresPermissions("cms:content:add")
    @Log(title = "新增文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsContent cmsContent) {
        cmsContent.setId(String.valueOf(System.currentTimeMillis()));
        cmsContent.setDel(0L);
        cmsContent.setCreateDate(new Date());
        cmsContent.setCreateBy(SecurityUtils.getUsername());
        return toAjax(cmsContentService.insertCmsContent(cmsContent));
    }

    /**
     * 修改文章
     */
//    @RequiresPermissions("cms:content:edit")
    @Log(title = "编辑文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsContent cmsContent) {

        cmsContent.setUpdateDate(new Date());
        cmsContent.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(cmsContentService.updateCmsContent(cmsContent));
    }

    /**
     * 删除文章
     */
//    @RequiresPermissions("cms:content:remove")
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(cmsContentService.deleteCmsContentByIds(ids));
    }

    @Log(title = "批量删除文章", businessType = BusinessType.DELETE)
    @PostMapping("/batch/delete")
    public AjaxResult batchDelete(@RequestBody String[] ids) {
        return toAjax(cmsContentService.deleteCmsContentByIds(ids));
    }
}
